package com.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.entity.Tag;
import com.google.gson.Gson;
import com.service.TagService;

import cn.hutool.http.HttpResponse;

/**
 * 标签控制层
 * @author MingCha Yang
 */
@Service
@RequestMapping("/tag")
public class TagController {
	@Resource
    private TagService tagService;
	@RequestMapping("/tagPage")
	public String gototagPage(ModelMap m) {
        List<Tag> tags= tagService.getTagList();
        m.put("tags", tags);
		return "Tag/tag-page";
	}
	@RequestMapping("/tag-Name")
	public void isTagName(String tagName,HttpServletResponse response) throws IOException {
		Tag tag = tagService.getTagBytagName(tagName);
		if(null != tag) {
			response.getWriter().write("error");
		}else {
			response.getWriter().write("success");
		}
	}
	@RequestMapping("/insertSubmit")
	public String addTag(String tagName,String tagDescription) {
		Tag tag = new Tag();
		tag.setTagName(tagName);
		tag.setTagDescription(tagDescription);
		tagService.addTag(tag);
		return "redirect:/tag/tagPage";
		
	}
	@RequestMapping("/delete/{tagId}")
	public String deleteTag(@PathVariable("tagId") Integer tagId) {
		tagService.deleteTagBytagId(tagId);
		return "redirect:/tag/tagPage";
	}
	@RequestMapping("/updateAjax")
	public void updateTagAjax(Integer tagId,HttpServletResponse response) throws Exception{
		Tag tag = tagService.getTagBytagId(tagId);
		Gson gson = new Gson();
		String json = gson.toJson(tag);
		response.getWriter().write(json);
	}
	@RequestMapping("/updateSubmit")
	public void update(Tag tag,HttpServletResponse response) throws IOException {
		tagService.updateTagBytagId(tag);
		response.getWriter().write("success");
	}
}
